/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
 *
 * @author Winzelric
 */
public class DecisionTree {
    ArrayList<ArrayList<String>> ar = new ArrayList<ArrayList<String>>();
    ArrayList<String> qwe = new ArrayList<String>();
    
    public void isi(String path){

      File toren = new File(path);
      int d = 0;
      int tesnum = 0;
      try {
      BufferedReader readIn = new BufferedReader(new FileReader(toren));
      String data;

      while ((data = readIn.readLine()) != null) {
          tesnum++;
          System.out.println(tesnum);
          System.out.println("ngehe");
           ArrayList<String> asd = new ArrayList<String>();
           System.out.println(data.length());
           while(d < data.length()){
               if (data.charAt(d)!= ','){
                 asd.add("" + data.charAt(d));
                   System.out.println(d);
               }
            d++;
            }

          ar.add(asd);
          d=0;


      }

      readIn.close();
    } catch (IOException e) {
          System.out.println("ngaco");
    }

      //System.out.println(ar);
      //selectionattribut (50,ar);
//    System.out.println(b);
    }


    public DecisionTree() {
        
    }

    public int getsumval(ArrayList<ArrayList<String>> ar , int attributke, String val){
    // untuk mencari jumlah suatu value pada attribut tertentu
        int sum = 0;
        int i = 0;
        while(i<ar.size()){
            if(ar.get(i).get(attributke).equals(val)){
             sum++;
            }
            i++;
        }
        return sum;
    }
    
    public int getattvaltrue (ArrayList<ArrayList<String>> ar , int attributke, String val){
    //untuk mencari jumlah dari nilai true yang dihasilkan oleh suatu attribut dengan nilai tertentu
        int sum = 0;
        int i = 0;
        while(i<ar.size()){
            if(ar.get(i).get(attributke).equals(val) && ar.get(i).get(0).equals("1")){
             sum++;
            }
            i++;
        }
        return sum;
    }
    
    public int getattvalfalse (ArrayList<ArrayList<String>> ar , int attributke, String val){
    //untuk mencari jumlah dari nilai false yang dihasilkan oleh suatu atribut dengan nilai tertentu
        int sum = 0;
        int i = 0;
        while(i<ar.size()){
            if(ar.get(i).get(attributke).equals(val) && ar.get(i).get(0).equals("0")){
             sum++;
            }
            i++;
        }
        return sum;
    }

    public float entropy (int sumtrue, int sumfalse){
        float a = ((float) sumtrue /((float)sumtrue +(float)sumfalse));
        float b = ((float) sumfalse /((float)sumtrue +(float) sumfalse));
        //System.out.println("nilai a"+ a);
        //System.out.println("nilai b"+ b);
        if (a == 0){
            float result = (float) (- (b * (Math.log(b) / Math.log(2))));
             return result;
        } else
            if(b == 0){
                float result = (float) (-(a * (Math.log(a) / Math.log(2))));
                 return result;
            } else
            {
            float result = (float) (-(a * (Math.log(a) / Math.log(2))) - (b * (Math.log(b) / Math.log(2))));
             return result;
            }
    }
    
    public float remainder (int attnum, ArrayList<ArrayList<String>> ar){
        float sum = 0;
        for (int i = 0; i < 2; i++){
            //sum = sum + (float) ((getattvaltrue(ar,attnum,("" + (char)(i))) + getattvalfalse(ar,attnum,("" + (char)(i))))/(getsumval(ar,0,"1") + getsumval(ar,0,"0")) * (float) (-((getattvaltrue(ar,attnum,("" + (char)(i))) /(getsumval(ar,0,"1") + getsumval(ar,0,"0"))) * (Math.log((getattvaltrue(ar,attnum,("" + (char)(i))) /(getsumval(ar,0,"1") + getsumval(ar,0,"0")))) / Math.log(2))) - ((getattvalfalse(ar,attnum,("" + (char)(i))) /(getsumval(ar,0,"1") + getsumval(ar,0,"0"))) * (Math.log((getattvalfalse(ar,attnum,("" + (char)(i))) /(getsumval(ar,0,"1") + getsumval(ar,0,"0")))) / Math.log(2)))));
            float a = ((float)getattvaltrue(ar,attnum,("" + i)) /((float)getattvaltrue(ar,attnum,("" + i)) +(float)getattvalfalse(ar,attnum,("" + i))));
            //nilai pi/(pi+ni)
            float b = ((float) (getattvalfalse(ar,attnum,("" + i))) /((float)(getattvaltrue(ar,attnum,("" + i))) +(float)(getattvalfalse(ar,attnum,("" + i)))));
            //nilai ni/(pi+ni)
            float c = (((float)(getattvaltrue(ar,attnum,("" + i))) +(float)(getattvalfalse(ar,attnum,("" + i)))));
            //nilai (pi+ni)
            int d = (int)(getattvaltrue(ar,attnum,("" + i)));
            //nilai pi
            int e = (int)(getattvalfalse(ar,attnum,("" + i)));
            //nilai ni
            sum = sum + ((c/((float)(getsumval(ar,0,"0") + (float) (getsumval(ar,0,"1"))))) * entropy(d,e));
            //System.out.println(d + " "+e);
        }
    return sum;
    }
    
    public float infogain (float rem, float entro){
        return (entro - rem);
    }
    
    public float[] daftarinfogain (int attsum){
    //mendaftar nilai dari information gain dari setiap attribut
      float[] infogaintotal = new float[attsum];
      for (int i = 1 ; i < attsum; i++){
          infogaintotal[i] = infogain (remainder(i,ar),entropy(getsumval(ar,0,"1"),getsumval(ar,0,"0")));
          //System.out.println("nilai remainder ke"+i+" "+remainder(i,ar));
          System.out.println(infogaintotal[i]);
      }
      return infogaintotal;
    }
    
    public float getminimuminfogain (float[] daftar){
        // mengambil nilai minimum infogain lalu mengganti nilai yang diambil dengan -1
        float tempmin = 0;
        int att = 1;
        tempmin = daftar[1];
         for (int i = 1 ; i < daftar.length; i++){
          if (daftar[i] < tempmin){
              tempmin = daftar[i];
              att = i ; 
          }
         }
         daftar[att] = -1;
        return att;
    }
    
  
  
  
  public static DecisionTreeModel generateModel(ArrayList<ArrayList<String>> arr) {
    return null;
  }
  
 public static void main (String args[]){
      DecisionTree tes = new DecisionTree();
      tes.isi("C:/Users/asus/Desktop/test.txt");
      System.out.println("minimum info gain adalah attribut ke "+ tes.getminimuminfogain(tes.daftarinfogain(23)));
      //tes.selectionattribut(50, tes.ar);
  }
 
  
}
